using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
    public PlayerControl inputControl;
    public Rigidbody2D rb;
    public PlayerAnimation playerAnimation;
    public PhysicsCheck physicsCheck;

    [Header("基本参数")]
    public Vector2 inputDirecton;
    public float speed;
    public float jumpForce;
    public float dashForce;

    public float hurtForce;

    public float attackSpeed;


    [Header("状态")]
    public bool isHurt;
    public bool isDead;
    public bool isAttack;
    public bool isAttack2;
    public bool isDash;

    [Header("卡肉，屏幕抖动")]
    public float shakeTime;
    public int attackPause;
    public float attackStrength;


    private void Awake() 
    {
        
       inputControl = new PlayerControl();
       rb = GetComponent<Rigidbody2D>();
       inputDirecton = inputControl.Gameplay.Move.ReadValue<Vector2>();
       inputControl.Enable();
       playerAnimation = GetComponent<PlayerAnimation>();
       physicsCheck = GetComponent<PhysicsCheck>();

        //攻击
        inputControl.Gameplay.Attack.started += PlayerAttack;

        //跳跃
        inputControl.Gameplay.Jump.started += Jump;

        //冲刺
        inputControl.Gameplay.Dash.started += Dash;

    }


    private void Update() 
    {
        inputDirecton = inputControl.Gameplay.Move.ReadValue<Vector2>();
        
    }
    
    private void FixedUpdate() 
    {
        if(!isDash)
            Move();

            
    }



    public void Move()
    {
        if(!isAttack)
            rb.velocity = new Vector2(inputDirecton.x * speed * Time.deltaTime,rb.velocity.y);
        
        int faceDir = (int)transform.localScale.x;
        if (inputDirecton.x > 0)
            faceDir = 1;
        if (inputDirecton.x <0)
            faceDir = -1;

        if(isAttack)
            rb.velocity = new Vector2(0,rb.velocity.y);

        if(isAttack2)
            rb.velocity = new Vector2(faceDir * attackSpeed,rb.velocity.y);
        //人物反转
        transform.localScale = new Vector3(faceDir,1,1);
    }
    private void PlayerAttack(InputAction.CallbackContext context)
    {
        playerAnimation.PlayerAttack();
        isAttack = true;
    }

    private void OnTriggerEnter2D(Collider2D other) {
    if(other.CompareTag("Enemy"))
        {
        AttackSense.Instance.HitPause(attackPause);
        AttackSense.Instance.CameraShake(shakeTime,attackStrength);
        if(transform.localScale.x > 0){
            other.GetComponent<Enemy>().GetHurt(Vector2.right);}
        else if(transform.localScale.x < 0){
            other.GetComponent<Enemy>().GetHurt(Vector2.left);}
        

        }
    }

    private void Jump(InputAction.CallbackContext context)
    {
        if (physicsCheck.isGround || physicsCheck.currentJumpnumbers > 0)
        {
            
            rb.velocity = new Vector2(rb.velocity.x,jumpForce);
            physicsCheck.currentJumpnumbers -= 1;
            // GetComponent<AudioDefination>()?.PlayAudioClip();

        }
    }

    private void Dash(InputAction.CallbackContext context)
    {
        
        if(!isDash)
        {
            isDash = true;
            rb.velocity = new Vector2(transform.localScale.x * dashForce,rb.velocity.y);
        }
        else
        {
            playerAnimation.DashAttack();
            rb.velocity = new Vector2(rb.velocity.x * 0.1f,rb.velocity.y);
        }

    }

    

}
